import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/HomeView.vue";


const originalPush = VueRouter.prototype.push
const originalReplace = VueRouter.prototype.replace
// push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
// replace
VueRouter.prototype.replace = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
  return originalReplace.call(this, location).catch(err => err)
}


Vue.use(VueRouter);

const routes = [
  {
    path: "/home",
    name: "home",
    component: Home,
  },
  {
    path:'/',
    redirect:'/home'
  },
  {
    path: "/list",
    name: "List",
    component: () => import("../views/ListView.vue"),
  },
  {
    path: "/cart",
    name: "Cart",
    component: () => import("../views/CartView.vue"),
  },
  {
    path: "/my",
    name: "My",
    component: () => import("../views/MyView.vue"),
  },
  {
    path: "/search",
    name: "Search",
    children:[
      {
        path: "/",
        name: "index",
        component: () => import("../views/search/SearchIndex.vue"),
      },
      {
        path: "list",
        name: "list",
        component: () => import("../views/search/SearchList.vue"),
      }
    ],
    component: () => import("../views/SearchView.vue"),
  },
  {
    path: "/detail",
    name: "Detail",
    meta:{
      keepAlive: true
    },
    component: () => import("../views/Detail.vue"),
  },
  {
    path: "/login",
    name: "Login",
    component: () => import("../views/login/Login.vue"),
  },
  {
    path: "/userLogin",
    name: "UserLogin",
    component: () => import("../views/login/UserLogin.vue"),
  },
  {
    path: "/register",
    name: "Register",
    component: () => import("../views/login/Register.vue"),
  },
  {
    path: "/recovery",
    name: "Recovery",
    component: () => import("../views/recovery/Recovery.vue"),
    children:[
      {
        path: "/",
        name: "index",
        component: () => import("../views/recovery/RecoveryIndex.vue"),
      },
      {
        path: "/btn",
        name: "btn",
        component: () => import("../views/recovery/RecoveryBtn.vue"),
      },
    ],
  },
  {
    path: "/path",
    name: "Path",
    children:[
      {
        path: "/",
        name: "PathIndex",
        component: () => import("../views/path/PathIndex.vue"),
      },
      {
        path: "pathList",
        name: "pathList",
        component: () => import("../views/path/PathList.vue"),
      },
    ],
    component: () => import("../views/Path.vue"),
  },
  {
    path: "/order",
    name: "Order",
    meta:{
      keepAlive:true,
    },
    component: () => import("../views/OrderView.vue"),
  },
  {
    path: "/payment",
    name: "Payment",
    component: () => import("../views/payment.vue"),
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});


// 导航守卫
router.beforeEach((to,from,next)=>{
  let   nextRouter=['Cart','Path','PathIndex',]
  // 是否登录中
  let userInfo=JSON.parse(localStorage.getItem('teaUserInfo'))

  // 判断即将进入的页面是否是需要验证的页面
  if(nextRouter.indexOf(to.name)>=0){
    if(!userInfo){
      router.push('/login')
    }
  }
  next()
})

export default router;
